package com.shunde.xunjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.shunde.xunjian.entity.VerificationReport;
import com.shunde.xunjian.mapper.dbnetworkshunde.ComplaintRegistrationMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.ReportOrdersMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.VerificationReportMapper;
import com.shunde.xunjian.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Calendar;
import java.util.UUID;

@Service
public class FileServiceImpl {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ReportOrdersMapper reportOrdersMapper;

    @Autowired
    private ComplaintRegistrationMapper complaintRegistrationMapper;

    @Autowired
    private VerificationReportMapper verificationReportMapper;

    // 指定存储文件夹的路径
    private static final String UPLOAD_FOLDER = "E:/xunjian/data/";

    Calendar cal = Calendar.getInstance();
    // 当前年
    int year = cal.get(Calendar.YEAR);
    // 当前月
    int month = cal.get(Calendar.MONTH) + 1;
    // 当前日
    int day = cal.get(Calendar.DATE);

    public Integer VerificationUpload(String verificationReportStr) {
        VerificationReport verificationReport = new Gson().fromJson(verificationReportStr, VerificationReport.class);
        Integer result = 0;
        if (!Utils.isEntityEmpty(verificationReport)) {
            QueryWrapper<VerificationReport> verificationReportQueryWrapper = new QueryWrapper<>();
            Integer maxId = verificationReportMapper.selectOne(verificationReportQueryWrapper.select("MAX(verification_id) AS verification_id")).getVerificationId();
            verificationReport.setVerificationId(maxId + 1);
            verificationReportMapper.insert(verificationReport);
            result = maxId + 1;
        }
        return result;
    }

    public String uploadFile(MultipartFile file, String tableName, String fieldName) throws IOException {
        // 检查是否收到了文件
        if (file.isEmpty()) {
            return "";
        }
        // 确保文件夹存在
        String pathname = UPLOAD_FOLDER + tableName + "/" + fieldName + "/" + (year) + "/" + (month) + "/" + (day) + "/";
        File folder = new File(pathname);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        // 生成文件名
        String fileName = UUID.randomUUID().toString() + ".jpg";
        // 将文件保存到指定路径
        Path targetFilePath = Paths.get(pathname, fileName);
        Files.copy(file.getInputStream(), targetFilePath, StandardCopyOption.REPLACE_EXISTING);
        System.out.println(pathname + fileName + "图片上传成功");
        return pathname.replace("E:/xunjian", "") + fileName;

    }

}
